package com.cnxjs.shs4et.controller;

import com.cnxjs.shs4et.common.Result;
import com.cnxjs.shs4et.entity.Forum;
import com.cnxjs.shs4et.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/forum")
public class ForumController {

    @Autowired
    private ForumService forumService;

    // 分页排序查询论坛与板块
    @GetMapping("/")
    public Result findAll(@RequestParam(defaultValue= "0") Integer page,
                          @RequestParam(defaultValue= "20") Integer size,
                          @RequestParam(defaultValue = "name") String sortRule){
        Sort sort = Sort.by(Sort.Direction.DESC, sortRule);
        Pageable pageable = PageRequest.of(page, size, sort);
        return forumService.findAll(pageable);
    }

    // 增加论坛或板块
    @PostMapping("/add")
    public Result add(@RequestBody Forum forum){
        return forumService.add(forum);
    }

    // 修改论坛或板块
    @PostMapping("/update")
    public Result update(@RequestBody Forum forum){
        return forumService.update(forum);
    }

    // 伪删除论坛或板块
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return forumService.delete(id);
    }
}
